package cn.yerly.sakura.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.yerly.sakura.domain.ItemImage;
import cn.yerly.sakura.repository.ItemImageRepository;
import cn.yerly.sakura.repository.search.ItemImageSearchRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing ItemImage.
 */
@RestController
@RequestMapping("/api")
public class ItemImageResource {

    private final Logger log = LoggerFactory.getLogger(ItemImageResource.class);

    @Inject
    private ItemImageRepository itemImageRepository;

    @Inject
    private ItemImageSearchRepository itemImageSearchRepository;

    /**
     * POST  /itemImages -> Create a new itemImage.
     */
    @RequestMapping(value = "/itemImages",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Void> create(@Valid @RequestBody ItemImage itemImage) throws URISyntaxException {
        log.debug("REST request to save ItemImage : {}", itemImage);
        if (itemImage.getId() != null) {
            return ResponseEntity.badRequest().header("Failure", "A new itemImage cannot already have an ID").build();
        }
        itemImageRepository.save(itemImage);
        itemImageSearchRepository.save(itemImage);
        return ResponseEntity.created(new URI("/api/itemImages/" + itemImage.getId())).build();
    }

    /**
     * PUT  /itemImages -> Updates an existing itemImage.
     */
    @RequestMapping(value = "/itemImages",
        method = RequestMethod.PUT,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Void> update(@Valid @RequestBody ItemImage itemImage) throws URISyntaxException {
        log.debug("REST request to update ItemImage : {}", itemImage);
        if (itemImage.getId() == null) {
            return create(itemImage);
        }
        itemImageRepository.save(itemImage);
        itemImageSearchRepository.save(itemImage);
        return ResponseEntity.ok().build();
    }

    /**
     * GET  /itemImages -> get all the itemImages.
     */
    @RequestMapping(value = "/itemImages",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<ItemImage> getAll() {
        log.debug("REST request to get all ItemImages");
        return itemImageRepository.findAll();
    }

    /**
     * GET  /itemImages/:id -> get the "id" itemImage.
     */
    @RequestMapping(value = "/itemImages/{id}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<ItemImage> get(@PathVariable Long id) {
        log.debug("REST request to get ItemImage : {}", id);
        return Optional.ofNullable(itemImageRepository.findOne(id))
            .map(itemImage -> new ResponseEntity<>(
                itemImage,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /itemImages/:id -> delete the "id" itemImage.
     */
    @RequestMapping(value = "/itemImages/{id}",
            method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public void delete(@PathVariable Long id) {
        log.debug("REST request to delete ItemImage : {}", id);
        itemImageRepository.delete(id);
        itemImageSearchRepository.delete(id);
    }

    /**
     * SEARCH  /_search/itemImages/:query -> search for the itemImage corresponding
     * to the query.
     */
    @RequestMapping(value = "/_search/itemImages/{query}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<ItemImage> search(@PathVariable String query) {
        return StreamSupport
            .stream(itemImageSearchRepository.search(queryString(query)).spliterator(), false)
            .collect(Collectors.toList());
    }
}
